package a4ny;

import java.util.ArrayList;
import java.util.Set;
import java.util.TreeMap;

public class BaselineTagger {
	TreeMap<String, TreeMap<String, Integer> > wordCounts;
	TreeMap<String, String> baselineWordTags;
	boolean countsChanged = false;
	
	public BaselineTagger(TreeMap<String, TreeMap<String, Integer> > wordCounts){
		baselineWordTags = new TreeMap<String, String>();
		this.wordCounts = wordCounts;
		initialize();
	}
	
	private void initialize(){
		// Chose the most frequent tag for each word as baseline
		Set<String> words = wordCounts.keySet();
		for(String w : words){
			TreeMap<String, Integer> countPerTag = wordCounts.get(w);
			Set<String> tags = countPerTag.keySet();
			int maxCount = 0;
			String maxCountTag = "";
			for(String t : tags){
				if(countPerTag.get(t) > maxCount){
					maxCount = countPerTag.get(t);
					maxCountTag = t;
				}
			}
			baselineWordTags.put(w, maxCountTag);
		}
		baselineWordTags.put("<unk>", "NN");
	}
	
	public ArrayList<String> tag(ArrayList<String> words){
		ArrayList<String> ret = new ArrayList<String>();
		for(String w : words){
			if(baselineWordTags.containsKey(w)){
				ret.add(baselineWordTags.get(w));
			}else{
				ret.add(baselineWordTags.get("<unk>"));
			}
		}
		return ret;
	}
}
